<template>
  <a-modal
    title="客户信息"
    :width="800"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="公司名称">
              <a-input placeholder="请选择客户公司名称" v-decorator="['companyName',{ rules: [{ required: true, message: '请输入公司名称' }] }]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="地址">
              <a-input placeholder="请选择客户地址" v-decorator="['address']" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="经营范围">
              <a-input placeholder="请选择客户经营范围" v-decorator="['businessScope']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="联系方式">
              <a-input placeholder="请填写客户联系方式" v-decorator="['contactWay']" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="邮箱">
              <a-input placeholder="请选择客户邮箱" v-decorator="['email']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="客户等级">
              <a-select
                v-decorator="['customerGrade']"
                placeholder="请选择客户等级"
                default-value="0">
                <a-select-option value="1">优质客户</a-select-option>
                <a-select-option value="2">普通客户</a-select-option>
                <a-select-option value="3">黑名单客户</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="备注">
              <a-input v-decorator="['note']" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'

// 表单字段
const fields = ['id', 'companyName', 'address', 'businessScope', 'contactsWay', 'email', 'customerGrade', 'note']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      form: this.$form.createForm(this)
    }
  },
  created () {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  }
}
</script>
